﻿using System;
using System.Collections.Generic;
using System.Text;
using EZBarTender.DataAccess.DAO;
using System.Text.RegularExpressions;

namespace EZBarTender.DataAccess.Mapping
{
    public class PrintManagerMapper
    {
        public static void Map(EZBT.PrintManagerDisplaysDataTable displays, EZBT.ProductBatchDataTable table, String type)
        {
            foreach (EZBT.ProductBatchRow row in table)
            {
                EZBT.PrintManagerDisplaysRow displayRow = displays.NewPrintManagerDisplaysRow();
                displayRow.BatchNo = row.BatchNo;
                displayRow.LocAisle = Convert.IsDBNull(row.LocAisle) ? 0 : row.LocAisle;
                displayRow.LocBlock = Convert.IsDBNull(row.LocBlock) ? 0 : row.LocBlock;
                displayRow.LocSeq = Convert.IsDBNull(row.LocSeq) ? 0 : row.LocSeq;
                displayRow.Upc = row.upc;
                displayRow.Type = getType(type, row.LabelWidth);
                if (row.LabelWidth == "SM")
                    displayRow.JobName = row.BatchNo + " Small Labels";
                else
                    displayRow.JobName = row.BatchNo;
                displays.AddPrintManagerDisplaysRow(displayRow);
            }

        }

        private static String getType(String type, String width)
        {
            if (width == "SM")
            {
                string[] temp = Regex.Split(type, "UP");
                if (temp[0] == "18")
                    return "32UP" + temp[1];
                else if (temp[0] == "6")
                    return "12UP" + temp[1];
            }
            else
            {
                string[] temp = Regex.Split(type, "UP");
                if (temp[0] == "32")
                    return "18UP" + temp[1];
                else if (temp[0] == "12")
                    return "6UP" + temp[1];
            }

            return type;
        }

        public static void Map(EZBT.PrintManagerDisplaysDataTable printManagerDisplaysDataTable, EZBT.DSDFutureBatchDataTable batchDT, string type)
        {
            foreach (EZBT.DSDFutureBatchRow row in batchDT)
            {
                EZBT.PrintManagerDisplaysRow displayRow = printManagerDisplaysDataTable.NewPrintManagerDisplaysRow();
                displayRow.BatchNo = row.BatchNo;
                displayRow.LocAisle = row.LocAisle;
                displayRow.LocBlock = row.LocBlock;
                displayRow.LocSeq = row.LocSeq;
                displayRow.Upc = row.UPC;
                displayRow.Type = getType(type, row.LabelWidth);
                if (row.LabelWidth == "SM")
                    displayRow.JobName = row.BatchNo + " Small Labels";
                else
                    displayRow.JobName = row.BatchNo;

                printManagerDisplaysDataTable.AddPrintManagerDisplaysRow(displayRow);
            }
        }
    }
}
